<?php

class Workset_Controller_Plugin_ActionSwitcher extends Zend_Controller_Plugin_Abstract {

    protected $_requestKey = null;

    protected $_actions = array();

	protected $_handleLoop = false;

    protected $_httpRedirect = true;

    protected $_httpRedirectCode = 301;

    function __construct($key = null, $actions = array()) {
        if (null !== $key) {
            $this->setRequestKey($key);
        }

        if (!empty($actions)) {
            foreach ($actions as $name => $params) {
                if (empty($params['action'])) {
                   continue;
                }

                $action = $params['action'];
                unset($params['action']);

                $controller = null;
                $module = null;
				$queryParams = array();

                if (isset($params['controller'])) {
                    $controller = $params['controller'];
                    unset($params['controller']);
                }

                if (isset($params['module'])) {
                    $module = $params['module'];
                    unset($params['module']);
                }

				if (isset($params['params']) && is_array($params['params'])) {
					$queryParams = $params['params'];
				}

                /*if (isset($params['internalParams']) && is_array($params['internalParams'])) {
					$queryParams = array_merge($queryParams, $params['internalParams']);
                    $queryParams['internalParams'] = array_keys($params['internalParams']);
				}*/

                $this->addAction($name, $action, $controller, $module, $queryParams);
            }
        }
    }

    function setRequestKey($key) {
        $this->_requestKey = (string)$key;
    }

    function doHttpRedirect($flag) {
        $this->_httpRedirect = (bool)$flag;
    }

    function setHttpRedirectCode($code) {
        $this->_httpRedirectCode = (int)$code;
    }

    function addAction($index, $action, $controller = null, $module = null, $params = array()) {
        $this->_actions[$index] = array(
            'action' => $action,
            'controller' => $controller,
            'module' => $module,
            'params' => $params,
        );
    }

    function postDispatch(Zend_Controller_Request_Abstract $request) {
        //проверка, вызывался ли плагин раньше
		if ($this->_handleLoop == true) {
			return;
		} else {
            $this->_handleLoop = true;
        }
		
        $operation = (string)$request->getParam($this->_requestKey);

        if (isset($this->_actions[$operation])) {

			//если переход через http-редирект на другой адрес
			if (!$this->_request->isXmlHttpRequest() && $this->_httpRedirect) {

				$helperUrl = new Workset_Controller_Action_Helper_Url();
				$url = $helperUrl->simple(
					$this->_actions[$operation]['action'],
					$this->_actions[$operation]['controller'],
					$this->_actions[$operation]['module'],
					$this->_actions[$operation]['params']
				);

				$helperRedirector = new Zend_Controller_Action_Helper_Redirector();
				$helperRedirector->gotoUrl($url, array(
                    'code' => $this->_httpRedirectCode
                ));
				
				return;

			} else {
				$request->setActionName($this->_actions[$operation]['action']);

				if (null !== $this->_actions[$operation]['controller']) {
					$request->setControllerName($this->_actions[$operation]['controller']);
				}

				if (null !== $this->_actions[$operation]['module']) {
					$request->setModuleName($this->_actions[$operation]['module']);
				}

				if (is_array($this->_actions[$operation]['params'])) {
					$request->setParams($this->_actions[$operation]['params']);
				}

				$request->setDispatched(false);

				return;
			}
        }

		$this->getResponse()->setException(new Zend_Exception('Undefined operation', 404));
    }

}

?>